$(function () {
    //统计视频数量
    function getCourseField(field) {
        var temp;
        $.ajax({
            url: '/OIndex/getCourseField',
            type: 'post',
            async: false,
            dataType:'json',
            data: {'field': field},
            success:function (data) {
                temp = data.data;
            }
        })
        return temp;
    };

    $('#uploadVideo').text(getCourseField('id').courseId);
    $('#lookNum').text(getCourseField('look_num').count);
    $('#collectNum').text(getCourseField('collect_num').count);
    $('#likeNum').text(getCourseField('like_num').count);

    //课程的待审核信息
    courseToExamine();
    function courseToExamine(){
        $.ajax({
            url: '/CourseRecord/getNumYes',
            type: 'post',
            dataType: 'json',
            data: {'type': 2},
            success:function (item) {
                $('#course_to_examine').text(item.data);
            }
        });
    };

    //商品的待审核信息
    productToExamine();
    function productToExamine(){
        $.ajax({
            url: '/CourseRecord/getNumYes',
            type: 'post',
            dataType: 'json',
            data: {'type': 3},
            success:function (item) {
                $('#product_to_examine').text(item.data);
            }
        });
    };

    //统计上架课程或商品
    function getGoOnlineCount(field, tabName, state, stateMsg) {
        var temp;
        $.ajax({
            url: '/OIndex/getGoOnlineCount',
            type: 'post',
            async: false,
            dataType:'json',
            data: {
                'field': field,
                'tabName': tabName,
                'state': state,
                'stateMsg': stateMsg,
            },
            success:function (data) {
                temp = data.data;
            }
        })
        return temp;
    };

    //上架课程
    $('#courseGoOnline').text(getGoOnlineCount('course_id', 'js_course' ,'course_status' , '2').count);
    $('#productGoOnline').text(getGoOnlineCount('product_id', 'js_product' ,'product_sate' , '1').count);


    //统计用户
    function getBeforeUser(startDate, endDate) {
        var temp;
        $.ajax({
            url: '/OIndex/getBeforeUser',
            type: 'post',
            async: false,
            dataType:'json',
            data: {
                'startDate': startDate,
                'endDate': endDate
            },
            success:function (data) {
                temp = data.data;
            }
        });
        if (temp == null){
            temp = 0;
        }
        return temp;
    };

    var data = new Date();

    var fullYear = data.getFullYear();
    var month = data.getMonth() + 1; // getMonth 方法返回 0-11，代表1-12月
    var nextDate = new Date(fullYear, month, 1)

    $('#beforeUserToday').text(getBeforeUser(data.toLocaleDateString() + ' 00:00:00', new Date(data.getTime() + 24 * 60 * 60 * 1000).toLocaleDateString() + ' 00:00:00'));
    $('#beforeUserYesterDay').text(getBeforeUser(new Date(data.getTime() - 24 * 60 * 60 * 1000).toLocaleDateString() + ' 00:00:00', data.toLocaleDateString() + ' 00:00:00'));
    $('#beforeUserThisMount').text(getBeforeUser(getFullDate(data.setDate(1)), getFullDate(nextDate)));
    $('#beforeUserCount').text(getBeforeUser('', ''));
    function getFullDate(targetDate) {
        var D, y, m, d;
        if (targetDate) {
            D = new Date(targetDate);
            y = D.getFullYear();
            m = D.getMonth() + 1;
            d = D.getDate();
        } else {
            console.log('请传入日期对象');
        }
        m = m > 9 ? m : '0' + m;
        d = d > 9 ? d : '0' + d;
        return y + '-' + m + '-' + d + ' 00:00:00';
    };

    $.ajax({
        url: '/OIndex/getCourseMonthSum',
        dataType:'json',
        success:function (data) {
            console.log(data.data.currentMonthSumSum);
            $('#monthSum').text(data.data.currentMonthSum);
            var monthSumRate = Math.round((data.data.currentMonthSum - data.data.lastMonthSum) / data.data.lastMonthSum * 100);
            console.log(monthSumRate);
            $('#monthSumRate').text(monthSumRate + "%");
        }
    });
    $.ajax({
        url: '/OIndex/getCourseAdopMonthSum',
        dataType:'json',
        success:function (data) {
            $('#monthAdoptSum').text(data.data.currentMonthSum);
            var monthSumRate = Math.round((data.data.currentMonthSum - data.data.lastMonthSum) / data.data.lastMonthSum * 100);
            $('#monthAdoptSumRete').text(monthSumRate + "%");
        }
    });
});